//
//  StatusCell.swift
//  Weibo
//
//  Created by 唐润欣 on 16/6/19.
//  Copyright © 2016年 唐润欣. All rights reserved.
//

import UIKit

//MARK: - 属性，构造函数，生命周期函数
class StatusCell: UITableViewCell {
    
    var viewModel: StatusViewModel? {
        
        didSet {
            
            originalView.viewModel = viewModel
            retweetedView.viewModel = viewModel
        }
    }
    
    /// 原创微博
    let originalView: OriginalView = OriginalView()
    
    /// 转发微博
    let retweetedView: RetweetedView = RetweetedView()
    
    /// 工具栏
    let toolBar: ToolBar = ToolBar()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .None
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - 设置UI
extension StatusCell {
    
    func setupUI() {
        
        //1. 添加子控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetedView)
        contentView.addSubview(toolBar)
        
//        originalView.backgroundColor = UIColor.whiteColor()
        
        //2. 自动布局
        originalView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentView)
            make.left.equalTo(contentView)
            make.right.equalTo(contentView)
        }
        
        retweetedView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(originalView.snp_bottom)
            make.left.equalTo(originalView)
            make.right.equalTo(originalView)
        }
        
        toolBar.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(retweetedView.snp_bottom)
            make.left.equalTo(contentView)
            make.right.equalTo(contentView)
            make.height.equalTo(40)
        }
        
        //3. 非常重要：tableViewCell的contentView默认没有做自动布局，如果需要自动计算行高，则需要设置contentView的自动布局
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self)
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.bottom.equalTo(toolBar)
        }
    }
}
